package com.salon.modules.makeup.controller;

import com.salon.common.annotation.SysLog;
import com.salon.common.http.response.ObjectResult;
import com.salon.common.http.response.PageResult;
import com.salon.common.http.response.ResultCode;
import com.salon.common.validator.AssertUtils;
import com.salon.common.validator.ValidatorUtils;
import com.salon.common.validator.group.AddGroup;
import com.salon.common.validator.group.DefaultGroup;
import com.salon.common.validator.group.UpdateGroup;
import com.salon.modules.makeup.entity.ServiceEntity;
import com.salon.modules.makeup.service.ServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Map;


/**
 * 美容服务
 *
 * @author lch 12345678@qq.com
 * @since 1.0.0 2023-10-09
 */
@RestController
@RequestMapping("makeup/service")
@Api(tags="美容服务")
public class ServiceController {

    @Autowired
    private ServiceService serviceService;

    @GetMapping("/list")
    @ApiOperation("分页")
    @RequiresPermissions("makeup:service:select")
    public PageResult<ServiceEntity> list(@ApiIgnore @RequestParam Map<String, Object> params){
        return serviceService.listServiceByPage(params);
    }

    @GetMapping("/info/{id}")
    @ApiOperation("详情")
    @RequiresPermissions("makeup:service:select")
    public ObjectResult<ServiceEntity> getById(@PathVariable("id") Long id){
        ServiceEntity data = serviceService.getById(id);
        return ObjectResult.success(data);
    }

    @PostMapping("/save")
    @ApiOperation("保存")
    @SysLog("保存美容服务")
    @RequiresPermissions("makeup:service:save")
    public ObjectResult<String> save(@RequestBody ServiceEntity entity){
        //效验数据
        ValidatorUtils.validateEntity(entity, AddGroup.class, DefaultGroup.class);
        serviceService.save(entity);
        return ObjectResult.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    @SysLog("修改美容服务")
    @RequiresPermissions("makeup:service:update")
    public ObjectResult<String> update(@RequestBody ServiceEntity entity){
        //效验数据
        ValidatorUtils.validateEntity(entity, UpdateGroup.class, DefaultGroup.class);
        serviceService.updateById(entity);
        return ObjectResult.success();
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    @SysLog("删除美容服务")
    @RequiresPermissions("makeup:service:delete")
    public ObjectResult<String> delete(@RequestBody Long[] ids){
        //效验数据
        if(CollectionUtils.isEmpty(Arrays.asList(ids))) {
            return ObjectResult.failed(ResultCode.FAIL, "参数错误");
        }
        serviceService.removeByIds(Arrays.asList(ids));
        return ObjectResult.success();
    }

}